<?php
class ControllerStep3 extends Controller {
	private $error = array();
	
	public function index() {		
		if ($this->request->isPost() && $this->validate()) {
			M('install');
			
			$this->model_install->mysql($this->request->post);
			
			$output  = '<?php' . "\n";
			$output .= 'define(\'ROOT\', \'' . PATH_ROOT . '\');' . "\n\n";
			$output .= '// HTTP' . "\n";
			$output .= 'define(\'HTTP_SERVER\', \'' . HTTP_V5CART . '\');' . "\n";
			$output .= 'define(\'HTTP_IMAGE\', \'' . HTTP_V5CART . 'image/\');' . "\n";			
			$output .= 'define(\'HTTP_ADMIN\', \'' . HTTP_V5CART . 'admin/\');' . "\n\n";

			$output .= '// HTTPS' . "\n";
			$output .= 'define(\'HTTPS_SERVER\', \'' . HTTP_V5CART . '\');' . "\n";
			$output .= 'define(\'HTTPS_IMAGE\', \'' . HTTP_V5CART . 'image/\');' . "\n\n";
						
			$output .= '// DIR' . "\n";
			$output .= 'define(\'DIR_APPLICATION\', \'' . DIR_V5CART . 'catalog/\');' . "\n";
			$output .= 'define(\'DIR_SYSTEM\', \'' . DIR_V5CART. 'system/\');' . "\n";
			$output .= 'define(\'DIR_DATABASE\', \'' . DIR_V5CART . 'system/database/\');' . "\n";
			$output .= 'define(\'DIR_LANGUAGE\', \'' . DIR_V5CART . 'catalog/language/\');' . "\n";
			$output .= 'define(\'DIR_TEMPLATE\', \'' . DIR_V5CART . 'themes/\');' . "\n";
			$output .= 'define(\'DIR_CONFIG\', \'' . DIR_V5CART . 'system/config/\');' . "\n";
			$output .= 'define(\'DIR_IMAGE\', \'' . DIR_V5CART . 'image/\');' . "\n";
			$output .= 'define(\'DIR_CACHE\', \'' . DIR_V5CART . 'system/cache/\');' . "\n";
			$output .= 'define(\'DIR_DOWNLOAD\', \'' . DIR_V5CART . 'download/\');' . "\n";
			$output .= 'define(\'DIR_LOGS\', \'' . DIR_V5CART . 'system/logs/\');' . "\n\n";
		
			$output .= '// DB' . "\n";
			$output .= 'define(\'DB_DRIVER\', \'mysql\');' . "\n";
			$output .= 'define(\'DB_HOSTNAME\', \'' . addslashes($this->request->post['db_host']) . '\');' . "\n";
			$output .= 'define(\'DB_USERNAME\', \'' . addslashes($this->request->post['db_user']) . '\');' . "\n";
			$output .= 'define(\'DB_PASSWORD\', \'' . addslashes($this->request->post['db_password']) . '\');' . "\n";
			$output .= 'define(\'DB_DATABASE\', \'' . addslashes($this->request->post['db_name']) . '\');' . "\n";
			$output .= 'define(\'DB_PREFIX\', \'' . addslashes($this->request->post['db_prefix']) . '\');' . "\n\n";
			$output .= "//stores\n";
			$output .= "// \$stores = array(\n";
			$output .= "	// 1 => array('http://www.example.com', 'https://www.example.com'),\n";
			$output .= "// );\n";
			$output .= '?>';				
		
			$file = fopen(DIR_V5CART . 'config.php', 'w');
		
			fwrite($file, $output);

			fclose($file);
	 
			$output  = '<?php' . "\n";
			$output .= 'define(\'ROOT\', \'' . PATH_ROOT . '\');' . "\n\n";
			$output .= '// HTTP' . "\n";
			$output .= 'define(\'HTTP_SERVER\', \'' . HTTP_V5CART . 'admin/\');' . "\n";
			$output .= 'define(\'HTTP_CATALOG\', \'' . HTTP_V5CART . '\');' . "\n";
			$output .= 'define(\'HTTP_IMAGE\', \'' . HTTP_V5CART . 'image/\');' . "\n\n";

			$output .= '// HTTPS' . "\n";
			$output .= 'define(\'HTTPS_SERVER\', \'' . HTTP_V5CART . 'admin/\');' . "\n";
			$output .= 'define(\'HTTPS_CATALOG\', \'' . HTTP_V5CART . '\');' . "\n";
			$output .= 'define(\'HTTPS_IMAGE\', \'' . HTTP_V5CART . 'image/\');' . "\n\n";

			$output .= '// DIR' . "\n";
			$output .= 'define(\'DIR_APPLICATION\', \'' . DIR_V5CART . 'admin/\');' . "\n";
			$output .= 'define(\'DIR_SYSTEM\', \'' . DIR_V5CART . 'system/\');' . "\n";
			$output .= 'define(\'DIR_DATABASE\', \'' . DIR_V5CART . 'system/database/\');' . "\n";
			$output .= 'define(\'DIR_LANGUAGE\', \'' . DIR_V5CART . 'admin/language/\');' . "\n";
			$output .= 'define(\'DIR_TEMPLATE\', \'' . DIR_V5CART . 'admin/view/template/\');' . "\n";
			$output .= 'define(\'DIR_CONFIG\', \'' . DIR_V5CART . 'system/config/\');' . "\n";
			$output .= 'define(\'DIR_IMAGE\', \'' . DIR_V5CART . 'image/\');' . "\n";
			$output .= 'define(\'DIR_CACHE\', \'' . DIR_V5CART . 'system/cache/\');' . "\n";
			$output .= 'define(\'DIR_DOWNLOAD\', \'' . DIR_V5CART . 'download/\');' . "\n";
			$output .= 'define(\'DIR_LOGS\', \'' . DIR_V5CART . 'system/logs/\');' . "\n";
			$output .= 'define(\'DIR_CATALOG\', \'' . DIR_V5CART . 'catalog/\');' . "\n\n";

			$output .= '// DB' . "\n";
			$output .= 'define(\'DB_DRIVER\', \'mysql\');' . "\n";
			$output .= 'define(\'DB_HOSTNAME\', \'' . addslashes($this->request->post['db_host']) . '\');' . "\n";
			$output .= 'define(\'DB_USERNAME\', \'' . addslashes($this->request->post['db_user']) . '\');' . "\n";
			$output .= 'define(\'DB_PASSWORD\', \'' . addslashes($this->request->post['db_password']) . '\');' . "\n";
			$output .= 'define(\'DB_DATABASE\', \'' . addslashes($this->request->post['db_name']) . '\');' . "\n";
			$output .= 'define(\'DB_PREFIX\', \'' . addslashes($this->request->post['db_prefix']) . '\');' . "\n";
			$output .= '?>';	

			$file = fopen(DIR_V5CART . 'admin/config.php', 'w');
		
			fwrite($file, $output);

			fclose($file);
			
			$this->redirect(HTTP_SERVER . 'index.php?route=step_4');
		}
		
		$this->data['action'] = HTTP_SERVER . 'index.php?route=step_3';
		
		if (isset($this->request->post['db_host'])) {
			$this->data['db_host'] = $this->request->post['db_host'];
		} else {
			$this->data['db_host'] = 'localhost';
		}
		
		if (isset($this->request->post['db_user'])) {
			$this->data['db_user'] = html_entity_decode($this->request->post['db_user']);
		} else {
			$this->data['db_user'] = '';
		}
		
		if (isset($this->request->post['db_password'])) {
			$this->data['db_password'] = html_entity_decode($this->request->post['db_password']);
		} else {
			$this->data['db_password'] = '';
		}

		if (isset($this->request->post['db_name'])) {
			$this->data['db_name'] = html_entity_decode($this->request->post['db_name']);
		} else {
			$this->data['db_name'] = '';
		}
		
		if (isset($this->request->post['db_prefix'])) {
			$this->data['db_prefix'] = html_entity_decode($this->request->post['db_prefix']);
		} else {
			$this->data['db_prefix'] = 'v5_';
		}
		
		if (isset($this->request->post['username'])) {
			$this->data['username'] = $this->request->post['username'];
		} else {
			$this->data['username'] = 'admin';
		}

		if (isset($this->request->post['password'])) {
			$this->data['password'] = $this->request->post['password'];
		} else {
			$this->data['password'] = '';
		}
		
		if (isset($this->request->post['email'])) {
			$this->data['email'] = $this->request->post['email'];
		} else {
			$this->data['email'] = '';
		}
		
		$this->children = array(
			'header',
			'footer'
		);
		
		$this->display('step_3.tpl');		
	}
	
	private function validate() {
		$p = true;
		
		if (!$this->request->post['db_host']) {
			$this->setMessage('error_db_host', 'Host required!');
			$p = false;
		}

		if (!$this->request->post['db_user']) {
			$this->setMessage('error_db_user', 'User required!');
			$p = false;
		}

		if (!$this->request->post['db_name']) {
			$this->setMessage('error_db_name', 'Database Name required!');
			$p = false;
		}
		
		if (!$this->request->post['username']) {
			$this->setMessage('error_username', 'Username required!');
			$p = false;
		}

		if (!$this->request->post['password']) {
			$this->setMessage('error_password', 'Password required!');
			$p = false;
		}

		if ((utf8_strlen($this->request->post['email']) > 96) || !preg_match('/^[^\@]+@.*\.[a-z]{2,6}$/i', $this->request->post['email'])) {
			$this->setMessage('error_email', 'Invalid E-Mail!');
			$p = false;
		}

		if (!$connection = @mysql_connect($this->request->post['db_host'], $this->request->post['db_user'], $this->request->post['db_password'])) {
			$this->setMessage('error_warning', 'Error: Could not connect to the database!');
			$p = false;
		} else {
			if (!@mysql_select_db($this->request->post['db_name'], $connection)) {
				$this->setMessage('error_warning', 'Error: Database does not exist!');
				$p = false;
			}
			
			mysql_close($connection);
		}
		
		if (!is_writable(DIR_V5CART . 'config.php')) {
			$this->setMessage('error_warning', 'Error: Could not write to config.php!');
			$p = false;
		}
	
		if (!is_writable(DIR_V5CART . 'admin/config.php')) {
			$this->setMessage('error_warning', 'Error: Could not write to admin/config.php!');
			$p = false;
		}	
		
    	return $p;		
	}
}
?>